<?php
/**
 * Created by PhpStorm.
 * User: fengbob
 * Date: 2018/2/12
 * Time: 11:17
 */

namespace app\controllers;

use app\librarys\PayProController;
use app\librarys\Tools;
use app\models\db\EShop;
use app\models\AdExt;
use app\models\EShopQuery;
use app\models\db\GoodsImage;
use app\models\File;
use yii\data\Pagination;
use yii\db\Exception;
use app\models\db\MainPage;


class MainpageController extends PayProController
{
    public $enableCsrfValidation = false;

    public function actionIndex()
    {
        $mini_program = '1';
        $eshop = EShop::findOne(['mini_programs_id' => $mini_program, 'is_main' => 1]);
//        var_dump($mainpage);
        return $this->render('index', ["eshop" => $eshop]);
    }

    /**
     * 获取主页配置信息
     * @return string
     */
    public function actionGetMainPage(){
//       return $this->render('index');
        $mini_programs = '1';
        $main_page = MainPage::findOne(['mini_programs_id'=> $mini_programs]);
        return $this->render('main_page',['main_page'=>$main_page]);
    }

    /**
     * @return string
     */
    public function actionSwiper()
    {
        $request = \Yii::$app->request;
        $name = $request->get('name');

        $mini_program_id = 1;
        $e_shop_id = 1;
        $ad_category_id = 1;

        $count = AdExt::getSwiperAdCount($mini_program_id, $e_shop_id, $ad_category_id, $name);
        $pagination = new Pagination([
            'totalCount' => $count
        ]);
        $items = AdExt::getSwiperAdList($pagination->page, $pagination->pageSize, $mini_program_id, $e_shop_id, $ad_category_id, $name);

        return $this->render('swiper',
            [
                'items' => $items,
                'name' => $name,
                'pagination' => $pagination
            ]);

    }

    public function actionSwiperPartial()
    {
        $this->layout = false;
        $request = \Yii::$app->request;
        $id = $request->post('id');
        $item = AdExt::getAdById($id);
        return $this->render('swiper-partial',
            [
                'item' => $item
            ]
        );
    }

    public function actionSwiperAdDeleteHandler()
    {
        $request = \Yii::$app->request;
        $id = $request->post('id');
        $result = AdExt::deleteAd($id);
        return $this->renderJson(null, $result ? 0 : 1, $result ? '操作成功' : '操作失败');
    }

    public function actionSwiperAdSaveHandler()
    {
        $request = \Yii::$app->request;
        $id = $request->post('id');
        $name = $request->post('name');
        $sort = $request->post('sort');
        $link_url = $request->post('link_url');
        $description = $request->post('description');
        $is_shown = $request->post('is_shown');
        $ad_category_id = '1';
        $mini_program_id = '1';
        $is_enabled = $request->post('is_enabled') == 'on' ? 1 : 0;
        $e_shop_id = '1';
        $img_path = $request->post('img_path');

        $image_id = '';
        if($img_path){
            $image_id = File::UploadCutImage($img_path);
        }
//        if (!empty($_FILES['uploadImg']['name'])) {
//            if ($_FILES['uploadImg']['error'] == 0) {
//                $imgCord = json_decode($request->post('imgCord'));
//                if ($imgCord) {
//                    $image_id = File::CutUploadImage($_FILES['uploadImg'], $imgCord->x, $imgCord->y, $imgCord->w, $imgCord->h, $imgCord->size_w, $imgCord->size_h);
//                } else {
//                    return $this->renderJson('', 1, '图片需要裁剪');
//                }
//                if (!$image_id) {
//                    return $this->renderJson('', 1, '图片格式错误');
//                }
//            } else {
//                $image_id = $request->post('image_id');
//            }
//        }

        $result = AdExt::saveAd($id, $e_shop_id, $name, $image_id, $link_url, $description, $is_shown, $ad_category_id, $sort, $mini_program_id, $is_enabled);
        return $this->renderJson(null, $result ? 0 : 1, $result ? '操作成功' : '操作失败');
    }

    public function actionMainPageSaveHandler(){
        $mini_programs = '1';
//        var_dump('in action');
        $request = \Yii::$app->request;
        $id = $request->post('id');

        $main_page = MainPage::findOne($id);
        if(empty($main_page))
            return $this->renderJson(null, 1, '操作失败');

        $main_page->hot_goods_num = $request->post('hot_goods_num');
        $main_page->hot_goods_sort = $request->post('hot_goods_sort');
        $main_page->newest_goods_num = $request->post('newest_goods_num');
        $main_page->newest_goods_sort = $request->post('newest_goods_sort');
        $main_page->promotion_goods_num = $request->post('promotion_goods_num');
        $main_page->promotion_goods_sort = $request->post('promotion_goods_sort');
        $main_page->free_express_goods_num = $request->post('free_express_goods_num');
        $main_page->free_express_goods_sort = $request->post('free_express_goods_sort');
        $main_page->recommend_goods_sort = $request->post('recommend_goods_sort');
        $main_page->recommend_goods_num = $request->post('recommend_goods_num');

        $result = $main_page->save();
        return $this->renderJson(null, $result ? 0 : 1, $result ? '操作成功' : '操作失败');


    }
}